package fi.profecons.csswindows.toolkit;

import java.awt.Dimension;

import fi.profecons.csswindows.ActionListener;
import fi.profecons.csswindows.Element;
import fi.profecons.csswindows.Container;

public class Frame extends Container implements ActionListener{
	
	private ContentPane contentPane;

	private String title;

	private boolean resizable;

	private boolean closable;

	private boolean fullscreen;

	private Frame parent = null;

	private boolean visible = true;

	private boolean maximized = false;

	private Dimension canvas;

	public Frame() {
		super();
		addCommand("new Window('" + getId() + "');");
		this.setContentPane(new ContentPane());
		this.canvas = new Dimension();
		this.setActionListener(this);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		addCommand("getElement('" + getId() + "').setTitle('" + title + "');");
	}

	public void add(Element element) {
		getContentPane().add(element);
	}

	public void setResizable(boolean b) {
		this.resizable = b;
		if (b) {
			addCommand("getElement('" + getId() + "').setResizable(true);");
		} else {
			addCommand("getElement('" + getId() + "').setResizable(false);");
		}
	}

	public void setClosable(boolean b) {
		this.closable = b;
		if (b) {
			addCommand("getElement('" + getId() + "').setClosable(true);");
		} else {
			addCommand("getElement('" + getId() + "').setClosable(false);");
		}
	}

	public void setFullscreen(boolean b) {
		this.fullscreen = b;
		if (b) {
			addCommand("getElement('" + getId() + "').setFullscreen(true);");
		} else {
			addCommand("getElement('" + getId() + "').setFullscreen(true);");
		}
	}

	public boolean isResizable() {
		return resizable;
	}

	

	public void show() {
		this.visible = true;
		addCommand("getElement('" + getId() + "').show();");
		this.refresh();
	}

	public void hide() {
		this.visible = false;
		addCommand("getElement('" + getId() + "').hide();");
	}

	public boolean isVisible() {
		return this.visible;
	}

	public void maximize() {
		this.maximized = true;
		addCommand("getElement('" + getId() + "').maximize()");
	}



	public void setWindowIcon(Image image) {
		if (!this.getAll().contains(image)) {
			add(image);
		}
		addCommand("getElement('" + getId() + "').setWindowIcon(getElement('"
				+ image.getId() + "'));");
	}

	public void setMinimizedIcon(Image image) {
		if (!this.getAll().contains(image)) {
			add(image);
		}
		addCommand("getElement('" + getId()
				+ "').setMinimizedIcon(getElement('" + image.getId() + "'));");
	}

	public void pack() {
		this.canvas.setSize(getContentPane().getWidth(), getContentPane().getHeight());
		addCommand("getElement('" + getId() + "').pack();");
	}

	public void refresh() {
		super.refresh();
		getContentPane().refresh();
	}

	public Dimension getCanvas() {
		return canvas;
	}

	public void setCanvas(Dimension canvas) {
		this.canvas = canvas;
	}
	
	public void setCanvasWidth(int width){
		this.canvas.width = width;
	}
	
	public void setCanvasHeight(int height){
		this.canvas.height = height;
	}

	public void handleAction(Element e, String action) {
		if(action.equals("resize")){
			this.refresh();
		}

	}

	public void handleAction(Element target, Element source, String action) {

	}

	public ContentPane getContentPane() {
		return contentPane;
	}
	public void setContentPane(ContentPane contentPane) {
		if(!getAll().contains(contentPane)){
			this.init(contentPane);
		}
		this.contentPane = contentPane;
		addCommand("getElement('" + getId() + "').setContentPane(getElement('"
				+ contentPane.getId() + "'));");
	}

}
